package com.feng.freader.view.sudo.activity;

//import static secondriver.sudo.activity.AppConstant.GAME_LEVEL_KEY;
//import static secondriver.sudo.activity.AppConstant.GAME_SHARE_PREFERENCES;
//import static secondriver.sudo.activity.AppConstant.GAME_PROGRESS_STORE_PREFIX;
//import static secondriver.sudo.activity.AppConstant.GAME_PROGRESS_STORE_KEY;
//
//import secondriver.sudo.R;
//import secondriver.sudo.bus.Game;
//import secondriver.sudo.bus.GameLevel;
//import secondriver.sudo.view.NineGridView;
import static com.feng.freader.view.sudo.activity.AppConstant.GAME_LEVEL_KEY;
import static com.feng.freader.view.sudo.activity.AppConstant.GAME_PROGRESS_STORE_KEY;
import static com.feng.freader.view.sudo.activity.AppConstant.GAME_PROGRESS_STORE_PREFIX;
import static com.feng.freader.view.sudo.activity.AppConstant.GAME_SHARE_PREFERENCES;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.feng.freader.R;
import com.feng.freader.view.sudo.bus.Game;
import com.feng.freader.view.sudo.bus.GameLevel;
import com.feng.freader.view.sudo.view.NineGridView;

public class GameActivity extends Activity {

	private static String TAG = "GameActivity";

	private NineGridView nineGridView;

	private SharedPreferences preferences;

	private int level;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_game_sudo);
		nineGridView = (NineGridView) findViewById(R.id.nineGridView);

		preferences = getSharedPreferences(GAME_SHARE_PREFERENCES, MODE_PRIVATE);

		Intent intent = getIntent();
		level = intent.getIntExtra(GAME_LEVEL_KEY, GameLevel.BASE.getV());

		String storeValue = intent.getStringExtra(GAME_PROGRESS_STORE_KEY);
		Log.i(TAG, "storeValue == " + storeValue);
		if (TextUtils.isEmpty(storeValue)) {
			nineGridView.setGame(Game.obtainGameFromGameLevel(GameLevel
					.get(level)));
		} else {
			nineGridView.setGame(Game.obtainGameFromString(storeValue));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.restart_item: {
			nineGridView.drawNewView();
			break;
		}
		case R.id.save_item: {
			String storeValue = Game.obtainGameToString(nineGridView.getGame());
			Log.i(TAG, "save storeValue == " + storeValue);
			if (preferences.edit()
					.putString(GAME_PROGRESS_STORE_PREFIX + level, storeValue)
					.commit()) {
				Toast.makeText(this, R.string.save_toast, Toast.LENGTH_LONG)
						.show();
			}
			break;
		}
		case R.id.exit_item: {
			finish();
			break;
		}
		case R.id.solution_item: {
			nineGridView.drawSolutionView();
			break;
		}
		default:
			break;
		}
		return true;
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
